home *** CD-ROM | disk | FTP | other *** search
- /* Functions for doing things to strings.
-
- Revision History:
-
- 91/07/02 AIH
- - Cast (-1) to ((size_t) -1) just to make sure it wasn't being interpreted
- as an integer
-
- 91/03/01 AIH
- - Added require/ensure statements to StrFit
-
- 91/01/21 AIH
- - Added brief comment describing this file
-
- 91/01/05 Ari Halberstadt (AIH)
- - Inserted this standard header in all files */
-
- #include <stddef.h>
- #include <string.h>
- #include "MemoryLib.h"
- #include "StringLib.h"
-
- /* true if string is valid and is not longer than length (-1 for any length) */
- Boolean StrValid(const char *str, size_t length)
- {
- return(MemValid(str) && (length == (size_t) -1 || strlen(str) < length));
- }
-
- /* fit string to width by truncating it and adding the extra character */
- void StrFit(CStr255 str, short maxwidth, char extra)
- {
- short width; /* width of string */
- short len; /* length of string */
- Str255 pstr;
-
- require(StrValid(str, sizeof(CStr255)));
- c2pstr(strcpy((char*)pstr, str));
- width = StringWidth(pstr);
- if (width > maxwidth) {
- width += CharWidth(extra);
- for (len = *pstr; len > 0 && width > maxwidth; len--)
- width -= CharWidth(str[len-1]);
- str[len] = extra;
- str[len+1] = 0;
- }
- ensure(width <= maxwidth);
- }
-